<template>
  <div class="app-container customer-div" style="overflow-x: auto">
    <!-- tabs标签页  -->
    <el-tabs v-model="listQuery.groupBy" type="card" @tab-click="handleClick1">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane label="军团" name="department"></el-tab-pane>
      <el-tab-pane label="业务员" name="user"></el-tab-pane>
      <el-tab-pane label="我的" name="my"></el-tab-pane>
    </el-tabs>
    <!-- 搜索栏  -->
    <el-form :inline="true" :model="listQuery" size="mini" label-width="50px">
      <el-form-item label="日期：">
        <el-date-picker
          v-model="listQuery.time"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" label-width="50px">
        <el-radio-group v-model="listQuery.groupDate">
          <el-radio-button label="day">按日</el-radio-button>
          <el-radio-button label="month">按月</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        label="军团:"
        label-width="50px"
        v-if="listQuery.groupBy == 'department'"
      >
        <el-cascader
          v-model="listQuery.departmentName"
          :options="data2"
          filterable
          @focus="getBuMenList"
          :props="{
            checkStrictly: true,
            value: 'departmentName',
            label: 'departmentName',
            emitPath: false,
          }"
          clearable
        >
        </el-cascader>
      </el-form-item>

      <el-form-item
        label="业务员:"
        label-width="70px"
        v-if="listQuery.groupBy == 'user'"
      >
        <el-select
          v-model="listQuery.userName"
          placeholder="请选择"
          allow-create
          @focus="getSalesmanList"
          filterable
          clearable
        >
          <el-option
            v-for="item in salesMemberList"
            :key="item.userName"
            :label="item.userName"
            :value="item.userName"
            :loading="salesmanLoading"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          style="margin-left: 10px"
          type="primary"
          @click="handleSearch"
        >
          查询
        </el-button>

        <el-button
          :loading="btnLoading"
          style="margin-left: 10px"
          @click="downloadExcel"
          type="primary"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div>
        <p class="inquiryNumber">{{ cardTotal.need }}</p>
        <span class="inquiryStr">需发货数量</span>
      </div>
      <div>
        <p class="inquiryNumber">{{ cardTotal.noPay }}</p>
        <span class="inquiryStr">需发货未付款笔数</span>
      </div>
      <div>
        <p class="inquiryNumber">{{ cardTotal.noPayNum }}</p>
        <span class="inquiryStr">需发货未付款数量</span>
      </div>
      <div>
        <p class="inquiryNumber">{{ cardTotal.planNum }}</p>
        <span class="inquiryStr">排货数量</span>
      </div>
      <div>
        <p class="inquiryNumber">{{ cardTotal.sendNum }}</p>
        <span class="inquiryStr">发货数量</span>
      </div>
      <div>
        <p class="inquiryNumber">{{ cardTotal.outSendNum }}</p>
        <span class="inquiryStr">超期发货数量</span>
      </div>
      <div>
        <p class="inquiryNumber">{{ cardTotal.outNoSendNum }}</p>
        <span class="inquiryStr">超期未发货数量</span>
      </div>
    </el-card>
    <!-- echarts图标 -->
    <div
      id="deliOvertimeChart"
      style="height: 400px; margin-top: 20px"
      v-loading="loadingChart"
    ></div>
    <!-- 数据列表 -->
    <el-table
      :data="tableData"
      style="width: 95%; margin-top: 20px"
      border
      height="500"
      :header-cell-style="{ background: '#fafafa', height: '48px', padding: 0 }"
    >
      <el-table-column prop="time" label="日期" min-width="100" align="center">
      </el-table-column>
      <el-table-column
        v-if="listQuery.groupBy == 'department'"
        prop="departmentName"
        label="军团"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="listQuery.groupBy == 'user'"
        prop="userName"
        label="业务员"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="need"
        label="需发货数量(吨)"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="noPay"
        label="需发货未付款笔数"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="noPayNum"
        label="需发货未付款数量(吨)"
        min-width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="planNum"
        label="排货数量(吨)"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="sendNum"
        label="发货数量(吨)"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="outSendNum"
        label="超期发货数量(吨)"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="outNoSendNum"
        label="超期未发货数量(吨)"
        min-width="100"
        align="center"
      >
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
const defaultListQuery = {
  time: [], //询价日期
  groupBy: "all",
  groupDate: "day", //类型(按日:day，按月:month)
  departmentName: "", //军团
  userName: "", //业务员
};
import { getOrderUsersList } from '@/api/partner'
import {getDeptList} from '@/api/management'
import { getDeliOvertimeReportAPI } from "@/api/allInLine/report.js";
import { formatDate } from "@/utils/date";
import { mapGetters } from "vuex";
import echarts from "echarts";
export default {
  name: "deliOvertimeReport",
  data() {
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "7天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "30天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      btnLoading: false,
      loadingChart: false,
      cardTotal: {}, //卡片合计
      // 表格数据
      tableData: [],
      // 折线图数据
      xList: [],
      needList: [], //需发货数量
      sendNumList: [], //发货数量
      planNumList: [], //排货数量
      outSendNumList: [], //超期发货数量
      outNoSendNumList: [], //超期未发货数量
      // 军团列表
      data2: [],
      dataLoading: false,
      // 业务员列表
      salesmanLoading: false,
      salesMemberList: [],
    };
  },
  computed: {
    ...mapGetters(["name", "mobile", "userId"]),
  },
  created() {
    let now = new Date();
    let today = formatDate(now, "yyyy-MM-dd");
    this.listQuery.time = [today, today];
    this.getDeliOvertimeReport();
  },
  mounted() {
    this.initChart();
  },
  methods: {
    //获取部门列表
    getBuMenList(){
      getDeptList().then((res)=>{
        this.data2 = this.forTree(res);
      })
    },
    //去除不要的children
    forTree(list) {
      let newList = list;
      if (newList.length) {
        for (let i = 0; i < newList.length; i++) {
          if (newList[i].child_cates && newList[i].child_cates.length > 0) {
            this.forTree(newList[i].child_cates);
          } else {
            delete newList[i].child_cates;
          }
        }
        return newList;
      }
    },
    // 获取业务员列表
    async getSalesmanList() {
      this.salesmanLoading = true;
      const res = await getOrderUsersList({ departmentType: "" });
      this.salesMemberList = res;
      this.salesmanLoading = false;
    },
    // 查询
    handleSearch() {
      this.needList = [];
      this.sendNumList = [];
      this.planNumList = [];
      this.outSendNumList = [];
      this.outNoSendNumList = [];
      this.getDeliOvertimeReport();
    },
    // 配置图表
    initChart() {
      const chart = echarts.init(document.getElementById("deliOvertimeChart"));
      const option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: [
            "需发货数量",
            "发货数量",
            "排货数量",
            "超期发货数量",
            "超期未发货数量",
          ],
          itemWidth: 60,
          itemHeight: 10,
          icon: "stack",
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.xList,
        },
        yAxis: {
          type: "value",
          name: "数量",
          position: "left",
        },
        series: [
          {
            name: "需发货数量",
            type: "line",
            stack: "Total",
            data: this.needList,
          },
          {
            name: "发货数量",
            type: "line",
            stack: "Total",
            data: this.sendNumList,
          },
          {
            name: "排货数量",
            type: "line",
            stack: "Total",
            data: this.planNumList,
          },
          {
            name: "超期发货数量",
            type: "line",
            stack: "Total",
            data: this.outSendNumList,
          },
          {
            name: "超期未发货数量",
            type: "line",
            stack: "Total",
            data: this.outNoSendNumList,
          },
        ],
      };
      chart.setOption(option);
    },
    //获取发货超时分析统计列表
    async getDeliOvertimeReport() {
      this.loadingChart = true;
      const res = await getDeliOvertimeReportAPI(this.listQuery);
      this.cardTotal = res.total;
      this.xList = res.xList;
      if (res.yList.length) {
        res.yList.forEach((item) => {
          this.needList.push(item.need);
          this.sendNumList.push(item.sendNum);
          this.planNumList.push(item.planNum);
          this.outSendNumList.push(item.outSendNum);
          this.outNoSendNumList.push(item.outNoSendNum);
        });
      }
      this.tableData = res.list;
      this.initChart();
      this.loadingChart = false;
    },
    // tab栏切换
    handleClick1() {
      this.needList = [];
      this.sendNumList = [];
      this.planNumList = [];
      this.outSendNumList = [];
      this.outNoSendNumList = [];
      this.listQuery.departmentName = "";
      this.listQuery.userName = "";
      this.getDeliOvertimeReport();
    },
    // 下载excle
    downloadExcel() {
      let params = JSON.parse(JSON.stringify(this.listQuery));
      params.isExport = 1;
      this.btnLoading = true;
      getDeliOvertimeReportAPI(params)
        .then((res) => {
          this.btnLoading = false;
          require.ensure([], () => {
            const { export_json_to_excel } = require("sheet/Export2Excel");
            const tHeader = res.title;
            const filterVal = res.columns;
            const list = res.list;
            const data = this.formatJson(filterVal, list);
            export_json_to_excel(tHeader, data, res.fileName);
          });
        })
        .catch(() => {
          this.btnLoading = false;
        });
    },
    // 下载表格数据格式化
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]));
    },
  },
};
</script>

<style scoped lang="scss">
.el-card /deep/ .el-card__body {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 90px;
  .inquiryNumber {
    text-align: center;
    font-size: 18px;
    font-weight: 500;
  }
  .inquiryStr {
    font-size: 14px;
    color: #ccc;
  }
}
</style>

